﻿using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.ViewModels;
using System.Windows;
using System.Windows.Controls;

namespace JiveMessenger.Views
{
    /// <summary>
    /// Description for RegisterWindow.
    /// </summary>
    public partial class RegisterWindow : Window
    {
        /// <summary>
        /// Initializes a new instance of the RegisterWindow class.
        /// </summary>
        public RegisterWindow()
        {
            InitializeComponent();
            Messenger.Default.Register<NotificationMessage>(this, msg =>
            {
                if (msg.Notification == "CloseWindow" && (msg.Sender == this.DataContext))
                    this.Close();
            });
            this.RegisterPasswordBox.Background.Opacity = 1;
            this.RepeatPasswordBox.Background.Opacity = 1;
        }


        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ((ViewModelRegisterWindow)this.DataContext).Cleanup();
        }

        private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox pb = (PasswordBox)sender;
            ((ViewModelRegisterWindow)this.DataContext).PasswordChangedCommand.Execute(pb.SecurePassword);

        }

        private void RegisterPasswordBox_GotFocus(object sender, RoutedEventArgs e)
        {
            ((PasswordBox)sender).Background.Opacity = 0;
        }

        private void RegisterPasswordBox_LostFocus(object sender, RoutedEventArgs e)
        {
            PasswordBox pb = (PasswordBox)sender;
            if (pb.Password.Length == 0)
                pb.Background.Opacity = 1;
        }

        private void RepeatPasswordBox_GotFocus(object sender, RoutedEventArgs e)
        {
            ((PasswordBox)sender).Background.Opacity = 0;
        }

        private void RepeatPasswordBox_LostFocus(object sender, RoutedEventArgs e)
        {
            PasswordBox pb = (PasswordBox)sender;
            if (pb.Password.Length == 0)
                pb.Background.Opacity = 1;
        }
        private void RepeatPasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox pb = (PasswordBox)sender;
            ((ViewModelRegisterWindow)this.DataContext).RepeatPasswordChangedCommand.Execute(pb.SecurePassword);
        }
    }
}